package com.example.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.entity.MessageEntity;
import com.example.service.MessageService;

@RequestMapping("/message")
@Controller
public class MessageController {
	
	@Autowired
	private MessageService messageService;
	
	/**
	 * 查询（日志）
	 * @param me
	 * @return
	 */
	@RequestMapping(path="/find",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> find(MessageEntity me) {
		Map<String, Object> relMap=new HashMap<String, Object>();
		me.setOffset((me.getPage() - 1) * me.getLimit());
		List<MessageEntity> list=messageService.find(me);
		int count=messageService.getCount(me);
		relMap.put("code", "0");
		relMap.put("msg", "success");
		relMap.put("count", count);
		relMap.put("data", list);
		return relMap;
	}
	
	/**
	 * 发信息（网络学生）
	 * @param me
	 * @return
	 */
	@RequestMapping(path="/addMessage",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addMessage(MessageEntity me){
		int count=messageService.addMessage(me);
		Map<String, Object> relMap=new HashMap<String, Object>();
		relMap.put("code", count>0?"0":"1");
		relMap.put("msg", count>0?"发送成功":"发送失败");
		return relMap;
	}
	
	/**
	 * 读取（消息日志）
	 * @param id
	 * @return
	 */
	@RequestMapping(path="/upMessage",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> upMessage(Integer id){
		int count=messageService.upMessage(id);
		Map<String, Object> relMap=new HashMap<String, Object>();
		relMap.put("code", count>0?"0":"1");
		relMap.put("msg", count>0?"读取成功":"读取失败");
		return relMap;
	}
	/**
	 * 消息提示
	 * @param me
	 * @return
	 */
	@RequestMapping("/getMessagePrompt")
	@ResponseBody
	public Map<String, Object> getMessagePrompt(MessageEntity me) {
		Map<String, Object> map = new HashedMap<String, Object>();
		int n=messageService.getMessagePrompt(me);
		map.put("message", n>0?"xiaoxi":"");
		map.put("data", 0);
		return map;
	}
}
